﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using e_commerce.BLL.Data;
using e_commerce.BLL.Models;

namespace e_commerce.BLL.Services.Enterprise
{
    public class GoodsService : IGoodsManager,IDisposable
    {
        private commerceDbContext _dbContext;
        public GoodsService()
        {
            _dbContext = new commerceDbContext();
        }
        public void CreateGoods(GoodsInfo goods)
        {
            List<Image> images = new List<Image>();
            foreach (var i in goods.Image)
            {
                images.Add(_dbContext.Image.SingleOrDefault(im => im.ImageId == i.ImageId));
            };
            GoodsInfo newGoods = new GoodsInfo()
            {
                GoodsName = goods.GoodsName,
                Price = goods.Price,
                Stock = goods.Stock,
                Details = goods.Details,
                Category = goods.Category,
                CommentNumber = 0,
                Deal = 0,
                State = (int)GoodsState.UnShelves,
                AddTime = DateTime.Now,
                Image = images
            };
            _dbContext.GoodsInfo.Add(newGoods);
            SaveChanges();
        }

        public void DeleteGoodsById(int goodsId)
        {
            var goods = _dbContext.GoodsInfo.SingleOrDefault(g => g.GoodsId == goodsId);
            if(goods.IsDelete == true)
            {
                throw new Exception("删除异常");
            }
            else if(goods.State == (int)GoodsState.Shelves)
            {
                throw new Exception("上架中商品无法删除");
            }
            goods.IsDelete = true;
            SaveChanges();
        }

        public void Dispose()
        {
            _dbContext.Dispose();
        }

        public void ModifyGoodsInfo(GoodsInfo goods)
        {
            var newGoods = GetGoodsById(goods.GoodsId);
            newGoods = goods;
            SaveChanges();
        }

        public void ModifyGoodsStockByGoodsId(int goodsId, int stock)
        {
            var goods = GetGoodsById(goodsId);
            goods.Stock = stock;
            SaveChanges();
        }
        public void OffShelvesGoods(int goodsId)
        {
            var goods = GetGoodsById(goodsId);
            if (goods.State == (int)GoodsState.OffShelves) throw new Exception("商品已上架");
            else if (goods.IsDelete)
            {
                throw new Exception("商品已被删除");
            }
            else
            {
                goods.State = (int)GoodsState.OffShelves;
            }
            SaveChanges();
        }
        public void OnShelvesGoods(int goodsId)
        {
            var goods = GetGoodsById(goodsId);
            if (goods.State == (int)GoodsState.Shelves) throw new Exception("商品已上架");
            else if (goods.IsDelete)
            {
                throw new Exception("商品已被删除");
            }
            else
            {
                goods.State = (int)GoodsState.Shelves;
            }
            SaveChanges();
        }
        public GoodsInfo GetGoodsById(int goodsId)
        {
            var goods = _dbContext.GoodsInfo.SingleOrDefault(g => g.GoodsId == goodsId&&!g.IsDelete);
            if (goods == null)
            {
                throw new Exception("商品不存在或已被删除");
            }
            return goods;
        }

        public IEnumerable<GoodsInfo> GetNewGoods(int count)
        {
            return _dbContext.GoodsInfo.OrderByDescending(g => g.AddTime).Where(g => !g.IsDelete).Take(count);
        }
        public int SaveImage(string src)
        {
            Image image = new Image()
            {
                ImageSrc = src,
            };
            _dbContext.Image.Add(image);
            SaveChanges();
            return image.ImageId;
        }
        public string GetGoodsState(int state)
        {
            string result = string.Empty;
            switch (state)
            {
                case 1:result = "未上架";break;
                case 2: result = "已上架"; break;
                case 3: result = "已下架"; break;
                case 4: result = "售罄"; break;
                default:result = "商品状态异常";break;
            }
            return result;
        }     
        public IEnumerable<GoodsInfo> GetGoodsByPage(int state ,int page,int pageSize,ref int total)
        {
            var goods = _dbContext.GoodsInfo.OrderByDescending(g => g.AddTime);
            if (state == 0)
            {
                total = _dbContext.GoodsInfo.Count();
                return goods.Where(g => !g.IsDelete).Skip((page - 1) * pageSize).Take(pageSize);
            }
            else
            {
                total = goods.Where(g => g.State == state).Count();
                return goods.Where(g => !g.IsDelete&&g.State == state).Skip((page - 1) * pageSize).Take(pageSize);
            }
        }
        public void SaveChanges()
        {
            _dbContext.SaveChanges();
        }
    }
}
